#!/bin/bash

# Soul Jigsaw Debian Package Builder
# 用于将应用打包为deb格式的脚本

set -e

APP_NAME="soul-jigsaw"
# 从版本文件读取当前版本号
VERSION_FILE="VERSION"
if [ -f "$VERSION_FILE" ]; then
    CURRENT_VERSION=$(cat "$VERSION_FILE")
else
    CURRENT_VERSION="1.0.0"
fi

# 自增版本号（仅增加修订版本号）
VERSION_PARTS=(${CURRENT_VERSION//./ })
MAJOR=${VERSION_PARTS[0]}
MINOR=${VERSION_PARTS[1]}
PATCH=${VERSION_PARTS[2]}
NEW_PATCH=$((PATCH + 1))
APP_VERSION="$MAJOR.$MINOR.$NEW_PATCH"

# 更新版本文件
echo "$APP_VERSION" > "$VERSION_FILE"

APP_DIR="$(cd "$(dirname "$0")" && pwd)"
BUILD_DIR="$APP_DIR/deb-build"
PYTHON_VERSION="3.9"

echo "开始构建 $APP_NAME 的Debian包，版本: $APP_VERSION..."

# 清理之前的构建
rm -rf "$BUILD_DIR"
mkdir -p "$BUILD_DIR"

# 创建Debian包目录结构
DEBIAN_DIR="$BUILD_DIR/$APP_NAME-$APP_VERSION"
mkdir -p "$DEBIAN_DIR/DEBIAN"
mkdir -p "$DEBIAN_DIR/usr/bin"
mkdir -p "$DEBIAN_DIR/usr/share/applications"
mkdir -p "$DEBIAN_DIR/usr/share/icons/hicolor/256x256/apps"
mkdir -p "$DEBIAN_DIR/usr/share/doc/$APP_NAME"
mkdir -p "$DEBIAN_DIR/usr/lib/$APP_NAME"
mkdir -p "$DEBIAN_DIR/etc/$APP_NAME"

# 复制应用源代码
echo "复制应用源代码..."
cp -r "$APP_DIR/src" "$DEBIAN_DIR/usr/lib/$APP_NAME/"
cp -r "$APP_DIR/config" "$DEBIAN_DIR/etc/$APP_NAME/"
cp "$APP_DIR/main.py" "$DEBIAN_DIR/usr/lib/$APP_NAME/"
cp "$APP_DIR/README.md" "$DEBIAN_DIR/usr/share/doc/$APP_NAME/"

# 创建控制文件
cat > "$DEBIAN_DIR/DEBIAN/control" << EOF
Package: $APP_NAME
Version: $APP_VERSION
Section: games
Priority: optional
Architecture: all
Depends: python3, python3-pyqt5, python3-opencv, python3-pil, python3-yaml, python3-numpy
Maintainer: Soul Jigsaw Team <soul-jigsaw@example.com>
Description: AI驱动的拼图游戏，帮助玩家通过完成拼图来探索内心世界
 AI-powered jigsaw puzzle game that helps players explore their inner world by completing puzzles.
EOF

# 创建安装后脚本
cat > "$DEBIAN_DIR/DEBIAN/postinst" << 'EOF'
#!/bin/bash
# 安装后脚本

# 设置权限
chmod +x /usr/bin/soul-jigsaw

# 更新桌面数据库
if [ -x /usr/bin/update-desktop-database ]; then
    update-desktop-database >/dev/null 2>&1 || true
fi

# 更新图标缓存
if [ -x /usr/bin/gtk-update-icon-cache ]; then
    gtk-update-icon-cache -f /usr/share/icons/hicolor >/dev/null 2>&1 || true
fi

exit 0
EOF

chmod 755 "$DEBIAN_DIR/DEBIAN/postinst"

# 创建卸载脚本
cat > "$DEBIAN_DIR/DEBIAN/prerm" << 'EOF'
#!/bin/bash
# 卸载前脚本

# 清理可能的缓存文件
rm -f /var/cache/soul-jigsaw/*.cache || true

exit 0
EOF

chmod 755 "$DEBIAN_DIR/DEBIAN/prerm"

# 创建启动脚本
cat > "$DEBIAN_DIR/usr/bin/soul-jigsaw" << 'EOF'
#!/bin/bash
# Soul Jigsaw 启动脚本

PYTHONPATH=/usr/lib/soul-jigsaw:$PYTHONPATH python3 /usr/lib/soul-jigsaw/main.py "$@"
EOF

chmod 755 "$DEBIAN_DIR/usr/bin/soul-jigsaw"

# 创建桌面文件
cat > "$DEBIAN_DIR/usr/share/applications/$APP_NAME.desktop" << EOF
[Desktop Entry]
Name=Soul Jigsaw
Comment=AI驱动的拼图游戏，帮助玩家通过完成拼图来探索内心世界
Exec=soul-jigsaw
Icon=$APP_NAME
Type=Application
Categories=Game;LogicGame;
Terminal=false
EOF

# 创建版权文件
cat > "$DEBIAN_DIR/usr/share/doc/$APP_NAME/copyright" << EOF
Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
Upstream-Name: $APP_NAME
Source: https://example.com/soul-jigsaw

Files: *
Copyright: 2023 Soul Jigsaw Team
License: GPL-3.0+

License: GPL-3.0+
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 .
 This package is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 .
 You should have received a copy of the GNU General Public License
 along with this program. If not, see <https://www.gnu.org/licenses/>.
 .
 On Debian systems, the full text of the GNU General Public License
 version 3 can be found in the file \`/usr/share/common-licenses/GPL-3'.
EOF

# 创建变更日志
cat > "$DEBIAN_DIR/usr/share/doc/$APP_NAME/changelog.Debian" << EOF
soul-jigsaw ($APP_VERSION) unstable; urgency=low

  * Version auto-incremented.

 -- Soul Jigsaw Team <soul-jigsaw@example.com>  $(date -R)
EOF

gzip -9 "$DEBIAN_DIR/usr/share/doc/$APP_NAME/changelog.Debian"

# 构建deb包
echo "构建deb包..."
dpkg-deb --build "$DEBIAN_DIR" "$BUILD_DIR/${APP_NAME}_${APP_VERSION}_all.deb"

echo "Debian包构建完成: $BUILD_DIR/${APP_NAME}_${APP_VERSION}_all.deb"